<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Products extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}
	
	public function _product_output($output = null)
	{
		$this->load->view('products',$output);
	}
	
	function index(){
		
		$this->_product_output((object)array('output' => '' , 'js_files' => array() , 'css_files' => array()));
	}
	
	function get_products(){
		
		if(!$this->session->userdata('username'))
        {
            $this->load->view('login');
        
		 } else {
		
		$crud = new grocery_CRUD();
		
		$crud->set_theme('flexigrid');
		$crud->set_table('products');
		$crud->set_relation('category_id','categories','category');
		$crud->display_as('category_id','Category Name');
		$crud->display_as('product_image_url','Product Image')->display_as('product','Product Title');
		$crud->set_subject('Products');

		$crud->required_fields('product_code','product','description','product_image_url');
		
		$crud->field_type('active', 'hidden', 1);
		
		$crud->callback_column('product_image_url',array($this,'_callback_product_image'));
		
		$crud->set_field_upload('product_image_url','assets/product_images');
		
		$crud->columns('product_id','product_code','product','description','product_image_url');
		
		 //$crud->unset_texteditor('description','full_text');
		
		
		$output = $crud->render();

		$this->_product_output($output);
		
		}
		
	}
	
	function _callback_product_image($value){
		
		return "<img src='".base_url().'assets/product_images/'.$value."'>";
		
	}
}

?>